package com.example.app2024.SurfaceGame;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class SurfaceViewTemplate extends SurfaceView implements Runnable
{

      private Thread mThread;
      private volatile boolean isRunning;  //控制繪制線程是否退出
      private Paint mPaint;
      private int mMinRadius;
      private int mMaxRadius;
      private int mRadius;
      private int mFlag;

      public SurfaceViewTemplate(Context context, AttributeSet attrs)
      {
            super(context, attrs);
            SurfaceHolder holder = this.getHolder();
            holder.addCallback(new SurfaceHolder.Callback()
            {
                  @Override
                  public void surfaceCreated(SurfaceHolder holder)
                  {
                        // 监听到Surface创建完毕 , 開啟線程
                        isRunning = true;
                        mThread = new Thread(SurfaceViewTemplate.this);
                        mThread.start();
                  }

                  @Override
                  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
                  {

                  }

                  @Override
                  public void surfaceDestroyed(SurfaceHolder holder)
                  {
                        isRunning = false;
                  }
            });

            setFocusable(true);
            setFocusableInTouchMode(true);
            setKeepScreenOn(true);

            initPaint();
      }

      private void initPaint()
      {
            mPaint = new Paint();
            mPaint.setDither(true);
            mPaint.setAntiAlias(true);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(6);
            mPaint.setColor(Color.GREEN);
      }

      @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh)
      {
            super.onSizeChanged(w, h, oldw, oldh);

            mMaxRadius = Math.min(w, h) / 2 - 20;
            mRadius = mMinRadius = 30;

      }

      @Override
      public void run()
      {
            while (isRunning)
            {
                  drawSelf();
            }
      }

      private void drawSelf()
      {
            Canvas canvas = null;

            try
            {
                  canvas = getHolder().lockCanvas();

                  if (canvas != null)
                  {
                        //  canvas.draw
                        drawCircle(canvas);
                  }
            } catch (Exception ex)
            {
                  ex.printStackTrace();

            } finally
            {
                  if (canvas != null)
                  {
                        getHolder().unlockCanvasAndPost(canvas);
                  }
            }
      }

      private void drawCircle(Canvas canvas)
      {
            canvas.drawColor(Color.WHITE);
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, mRadius, mPaint);

            if (mRadius >= mMaxRadius)
            {
                  mFlag = -1;
            }
            else if (mRadius <= mMinRadius)
            {
                  mFlag = 1;
            }

            mRadius += mFlag * 2;
      }
}
